/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.cmict.data.form.controller.admin;
import cn.hutool.core.collection.CollUtil;
import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.framework.common.pojo.PageParam;
import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.form.api.convert.SysCategoryConvert;
import com.cmict.data.form.api.entity.SysCategory;
import com.cmict.data.form.api.entity.dataobject.SysCategoryItemRespVO;
import com.cmict.data.form.api.service.SysCategoryService;
import com.cmict.data.system.api.dept.DeptApi;
import com.cmict.data.system.api.dept.PostApi;
import com.cmict.data.system.api.dept.dto.DeptRespDTO;
import com.cmict.data.system.api.user.AdminUserApi;
import com.cmict.data.system.api.user.dto.AdminUserRespDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import static com.cmict.data.framework.common.pojo.CommonResult.error;
import static com.cmict.data.framework.common.pojo.CommonResult.success;


/**
 * 类别表
 *
 * @author pigx code generator
 * @date 2022-03-14 11:37:08
 */
@RestController
@Validated
@RequestMapping("/form/category" )
@Api(value = "syscategory", tags = "类别表管理")
public class SysCategoryController {

    @Resource
    private SysCategoryService sysCategoryService;

    @Resource
    private AdminUserApi adminUserApi;

    @Resource
    private DeptApi deptApi;

    @Resource
    private PostApi postApi;

    /**
     * 分页查询
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('form:syscategory:query')")
    //@DataPermission(enable=false)
    public CommonResult<PageResult<SysCategoryItemRespVO>> getSysCategoryPage(PageParam pageParam) {
        PageResult<SysCategory> pageResult = sysCategoryService.getFormPage(pageParam);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(new PageResult<>(pageResult.getTotal())); // 返回空
        }
        // 拼接结果返回
        List<SysCategoryItemRespVO> categoryList = new ArrayList<>(pageResult.getList().size());
        pageResult.getList().forEach( category -> {
            SysCategoryItemRespVO sysCategoryItemRespVO = SysCategoryConvert.INSTANCE.convert(category);
            CommonResult<AdminUserRespDTO> result = adminUserApi.getUser(Long.valueOf(category.getCreator()));
            AdminUserRespDTO user = result.getCheckedData();
            if(null != user){
                sysCategoryItemRespVO.setCreatorName(user.getUsername());
                categoryList.add(sysCategoryItemRespVO);
            }
        });
        return success(new PageResult<>(categoryList, pageResult.getTotal()));
    }

    /**
     * 根据多个登录id查询列表
     * @return
     */
    @ApiOperation(value = "根据多个登录id查询列表", notes = "根据多个登录id查询列表")
    @PostMapping("/getUserList")
    //@DataPermission(enable=false)
    public CommonResult<List<AdminUserRespDTO>> getUserList(@RequestBody Collection<Long> names) {
        List<AdminUserRespDTO> result = adminUserApi.getUsersByUserNamesInnerMap(names);
        return success(result);
    }



    /**
     * 根据多个岗位id查询列表
     * @return
     */
    @ApiOperation(value = "根据多个岗位id查询列表", notes = "根据多个岗位id查询列表")
    @PostMapping("/gePostList")
    //@DataPermission(enable=false)
    public CommonResult<List<Map<String, Object>>> gePostList(@RequestBody Collection<Long> names) {
     List<Map<String, Object>> result = postApi.getPostsByPostNamesMap(names);
        return success(result);
    }


    /**
     * 根据多个登录名称查询列表
     * @return
     */
    @ApiOperation(value = "根据多个部门id查询列表", notes = "根据多个部门id查询列表")
    @PostMapping("/geDeptList")
    //@DataPermission(enable=false)
    public CommonResult<List<DeptRespDTO>> geDeptList(@RequestBody Collection<Long> ids) {
      return deptApi.getDeptsByIds(ids);
    }

    /**
     * 获取所有分类
     * @return
     */
    @ApiOperation(value = "获取所有分类", notes = "获取所有分类")
    @GetMapping("/all")
    @PreAuthorize("@ss.hasPermission('form:syscategory:query')")
    public CommonResult<List<SysCategory>> getSysCategoryAll() {
        List<SysCategory> list = sysCategoryService.getCategoryAll();
        if (CollUtil.isEmpty(list)) {
            return success(list); // 返回空
        }
        return success(list);
    }


    /**
     * 通过id查询类别表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@ss.hasPermission('form:syscategory:query')")
    public CommonResult getById(@PathVariable("id" ) Long id) {
        return success(sysCategoryService.getSysCategory(id));
    }

    /**
     * 新增类别表
     * @param sysCategory 类别表
     * @return R
     */
    @ApiOperation(value = "新增类别表", notes = "新增类别表")
    @PostMapping
    @PreAuthorize("@ss.hasPermission('form:syscategory:create')")
    public CommonResult save(@RequestBody SysCategory sysCategory) {
        Long id = sysCategoryService.createSysCategory(sysCategory);
        if(0L == id){
            return error(999,"排序字段不能为空！");
        }else{
            return success(id);
        }
    }

    /**
     * 修改类别表
     * @param sysCategory 类别表
     * @return R
     */
    @ApiOperation(value = "修改类别表", notes = "修改类别表")
    @PostMapping("/edit" )
    @PreAuthorize("@ss.hasPermission('form:syscategory:update')")
    public CommonResult updateById(@RequestBody SysCategory sysCategory) {
        sysCategoryService.updateSysCategory(sysCategory);
        return success(true);
    }

    /**
     * 通过id删除类别表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除类别表", notes = "通过id删除类别表")
    @GetMapping("/delete/{id}" )
    @PreAuthorize("@ss.hasPermission('form:syscategory:delete')")
    public CommonResult removeById(@PathVariable Long id) {
        return success(sysCategoryService.deleteSysCategory(id));
    }


}
